#!/usr/bin/Rscript
# 2016.11.22 

library(clusterProfiler)
library(openxlsx)
library(org.Hs.eg.db)
library(RDAVIDWebService)
library(DOSE)
library(igraph)
library(KEGG.db)


# 做do疾病富集注释，效果极差
do.do <- function(genelist){
  do <- enrichDO(genelist$ENTREZID, pvalueCutoff = 0.8, pAdjustMethod = "BH")
  do <- setReadable(do, 'org.Hs.eg.db')
  do <- as.data.frame(do)
  return(do)
}
# 做NCG的疾病富集注释，效果极差
do.ncg <- function(genelist){
  ncg <- enrichNCG(genelist$ENTREZID, pvalueCutoff = 0.8, pAdjustMethod = "BH")
  ncg <- setReadable(ncg, 'org.Hs.eg.db')
  ncg <- as.data.frame(ncg)
  return(ncg)
}
# 做kegg通路富集注释，kegg无法使用setReadable，所以ID没法转化为gene name这个很不爽
do.kegg <- function(genelist){
  kegg <- enrichKEGG(genelist$ENTREZID, pvalueCutoff = 0.05, pAdjustMethod = "BH")
  # kegg <- setReadable(kegg, 'org.Hs.eg.db')
  kegg <- as.data.frame(kegg)
  return(kegg)
}
# 做 GO Term的注释
do.go <- function(gene.df){
  real.do <- function(gene.df, type){
    cat(paste(as.character(type), "\t", sep = ""))
    ego2 <- enrichGO(gene         = gene.df$ENSEMBL,
                     OrgDb         = org.Hs.eg.db,
                     keytype       = 'ENSEMBL',
                     ont           = as.character(type),
                     pAdjustMethod = "BH",
                     pvalueCutoff  = 0.01,
                     qvalueCutoff  = 0.05)
    # ego2 <- setReadable(ego2, OrgDb = org.Hs.eg.db)
    return(as.data.frame(ego2))
  }
  
  cc <- real.do(gene.df, "CC")
  bp <- real.do(gene.df, "BP")
  mf <- real.do(gene.df, "MF")
  
  if(nrow(cc) == 0){
    stop("cc没有富集")
  }else if(nrow(bp) == 0){
    stop("bp没有富集")
  }else if(nrow(mf) == 0){
    stop("mf没有富集")
  }
  
  cc$type <- "cellular component"
  bp$type <- "biological process"
  mf$type <- "molecular funtion"
  
  all <- rbind(cc, bp)
  all <- rbind(all, mf)
  
  return(all)
}



setwd("F:/cuffdiff/")

cyto <- read.xlsx("mirandaBC_superdiff.xlsx", sheet = 4, colNames = F)

y <- unique(cyto[,1])
wb = createWorkbook()
for(i in 1:length(y)){
  test <- cyto[cyto[,1] == as.character(y[i]) & cyto[,3] == "mrna", 2]
  eg <- bitr(test, fromType = "SYMBOL", toType = c("ENTREZID", "ENSEMBL"), OrgDb = "org.Hs.eg.db")
  kegg <- do.kegg(eg)
  addWorksheet(wb, as.character(y[i]))
  writeData(wb, as.character(y[i]),kegg)
}
saveWorkbook(wb, "superdiffkegg.xlsx", overwrite = T)
rm(wb)

wb = createWorkbook()
for(i in 1:length(y)){
  test <- cyto[cyto[,1] == as.character(y[i]) & cyto[,3] == "mrna", 2]
  eg <- bitr(test, fromType = "SYMBOL", toType = c("ENTREZID", "ENSEMBL"), OrgDb = "org.Hs.eg.db")
  kegg <- do.do(eg)
  addWorksheet(wb, as.character(y[i]))
  writeData(wb, as.character(y[i]),kegg)
}
saveWorkbook(wb, "superdiffdo.xlsx", overwrite = T)
rm(wb)


wb = createWorkbook()
for(i in 1:length(y)){
  test <- cyto[cyto[,1] == as.character(y[i]) & cyto[,3] == "mrna", 2]
  eg <- bitr(test, fromType = "SYMBOL", toType = c("ENTREZID", "ENSEMBL"), OrgDb = "org.Hs.eg.db")
  kegg <- do.ncg(eg)
  addWorksheet(wb, as.character(y[i]))
  writeData(wb, as.character(y[i]),kegg)
}
saveWorkbook(wb, "superdiffncg.xlsx", overwrite = T)
rm(wb)

wb = createWorkbook()
for(i in 1:length(y)){
  test <- cyto[cyto[,1] == as.character(y[i]) & cyto[,3] == "mrna", 2]
  eg <- bitr(test, fromType = "SYMBOL", toType = c("ENTREZID", "ENSEMBL"), OrgDb = "org.Hs.eg.db")
  kegg <- do.go(eg)
  if(nrow(kegg) > 0){
    addWorksheet(wb, as.character(y[i]))
    writeData(wb, as.character(y[i]),kegg)
  }
  
}
saveWorkbook(wb, "superdiffgo.xlsx", overwrite = T)
rm(wb)

